﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagChina.CCHCS.ViewModels;
using HomagGroup.Base.UI;
using HomagGroup.BaseUI.Router;
using Proxy;
using System;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS
{
    public class SettingsPositionDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private readonly InsertInfoHandle Handle_Insert;
        private readonly SearchInfoHandle Handle_Search;
        private readonly ModifyInfoHandle Handle_Modify;
        private readonly CommonHandle commonHandle;
        public RelayCommand ApplyCommand { get; }

        #endregion Handle

        #region 属性

        private int _ID;
        private string _Positioncode;
        private float? _Baseheight;
        private float? _Xposition;
        private float? _Yposition;
        private int? _Rotate_Angle;
        private bool _IsEdit;

        public string Positioncode
        {
            get => _Positioncode;
            set { _Positioncode = value; OnPropertyChanged(); }
        }

        public float? Baseheight
        {
            get => _Baseheight;
            set { _Baseheight = value; OnPropertyChanged(); }
        }

        public float? Xposition
        {
            get => _Xposition;
            set { _Xposition = value; OnPropertyChanged(); }
        }

        public float? Yposition
        {
            get => _Yposition;
            set { _Yposition = value; OnPropertyChanged(); }
        }

        public int? Rotate_Angle
        {
            get => _Rotate_Angle;
            set { _Rotate_Angle = value; OnPropertyChanged(); }
        }

        public bool IsEdit
        {
            get => _IsEdit;
            set { _IsEdit = value; OnPropertyChanged(); }
        }

        public int ID
        {
            get => _ID;
            set { _ID = value; OnPropertyChanged(); }
        }

        #endregion 属性

        //构造函数
        public SettingsPositionDialogViewModel(PositionConfigModel SelectItem, Boolean IsEdit)
        {
            this.IsEdit = IsEdit;
            ApplyCommand = new RelayCommand(Apply);
            commonHandle = new CommonHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            if (SelectItem != null)
            {
                ID = SelectItem.Id;
                Positioncode = SelectItem.Positioncode;
                Baseheight = SelectItem.Baseheight;
                Xposition = SelectItem.Xposition;
                Yposition = SelectItem.Yposition;
                Rotate_Angle = SelectItem.Rotate_Angle;
            }
        }

        /// <summary>
        /// 提交函数
        /// </summary>
        private void Apply()
        {
            var t = new T_CCHS_Position
            {
                Positioncode = Positioncode,
                Baseheight = Baseheight,
                Xposition = Xposition,
                Yposition = Yposition,
                Rotate_Angle = Rotate_Angle,
                CreationDate = DateTime.Now
            };
            if (IsEdit)
            {
                if (Handle_Modify.ModifyLibraryConfig(t, this.ID))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Congratulations"));
                    commonHandle.CloseDiaLog(GetLocalText("SettingsPositionDialog"));
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Add failed"));
                }
            }
            else
            {
                if (Handle_Insert.InsertLibraryConfigHandle(t))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Congratulations"));
                    commonHandle.CloseDiaLog(GetLocalText("SettingsPositionDialog"));
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Add failed"));
                }
            }
            //调用SettingsViewModel的query
            (NavigateServices.Instance.CurrentViewModel as SettingsViewModel).Query();
        }
    }
}